package org.kmp.project.main.fav
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import org.jetbrains.compose.resources.vectorResource
import org.kmp.project.resources.Res
import org.kmp.project.resources.v_home



class Favorite{

    val viewModel =FavoriteViewModel()

    @Composable
    fun screen(modifier: Modifier = Modifier) {

        Column(
            modifier = modifier
                .fillMaxSize()
                .padding(16.dp)
        ) {
            Text("我的收藏", modifier = Modifier.padding(bottom = 16.dp))

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                OutlinedTextField(
                    value = viewModel.newItemText,
                    onValueChange = { viewModel.updateNewItemText(it) },
                    modifier = Modifier.weight(1f),
                    label = { Text("添加收藏") }
                )

                Button(onClick = { viewModel.addFavorite() }) {
                    Text("添加")
                }
            }

            if (viewModel.favorites.isEmpty()) {
                Text(
                    "暂无收藏，添加一个吧！",
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(top = 24.dp),
                    textAlign = TextAlign.Center
                )
            } else {
                LazyColumn(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(top = 16.dp)
                ) {
                    items(viewModel.favorites) { item ->
                        Row(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(vertical = 8.dp),
                            horizontalArrangement = Arrangement.SpaceBetween,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Text(item)
                            Button(onClick = { viewModel.removeFavorite(item) }) {
                                Image(
                                    imageVector = vectorResource(Res.drawable.v_home),
                                    contentDescription = "删除"
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}

